Skip to content

Remove the #[no_sanitize] attribute in favor of #[sanitize(xyz = "on|off")] #142681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

1c3t3a
Copy link
Member

@1c3t3a 1c3t3a commented Jun 18, 2025

This came up during the sanitizer stabilization (#123617). Instead of a #[no_sanitize(xyz)] attribute, we would like to have a #[sanitize(xyz = "on|off")] attribute, which is more powerful and allows to be extended in the future (instead
of just focusing on turning sanitizers off). The implementation is done according to what was discussed on Zulip).

The new attribute also works on modules, traits and impl items and thus enables usage as the following:

#[sanitize(address = "off")]
mod foo {
    fn unsanitized(..) {}

    #[sanitize(address = "on")]
    fn sanitized(..) {}
}

trait MyTrait {
  #[sanitize(address = "off")]
  fn unsanitized_default(..) {}
}

#[sanitize(thread = "off")]
impl MyTrait for () {
    ...
}

r? @rcvalle

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-rustc-dev-guide Area: rustc-dev-guide PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 18, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 18, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

The rustc-dev-guide subtree was changed. If this PR only touches the dev guide consider submitting a PR directly to rust-lang/rustc-dev-guide otherwise thank you for updating the dev guide with your changes.

cc @BoxyUwU, @jieyouxu, @Kobzol

triagebot.toml has been modified, there may have been changes to the review queue.

cc @davidtwco, @wesleywiser

Some changes occurred in src/doc/unstable-book/src/language-features/no-sanitize.md

cc @rust-lang/project-exploit-mitigations, @rcvalle

Some changes occurred in tests/codegen/sanitizer

cc @rcvalle

Some changes occurred in tests/ui/sanitizer

cc @rcvalle

Some changes occurred in compiler/rustc_codegen_ssa/src/codegen_attrs.rs

cc @jdonszelmann

@rcvalle
Copy link
Member

rcvalle commented Jun 18, 2025

Thank you very much for your time and work on this, @1c3t3a! Much appreciated.

@rcvalle
Copy link
Member

rcvalle commented Jun 18, 2025

@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from 5f8e6ee to 0302657 Compare June 18, 2025 16:03
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from 0302657 to a2216db Compare June 18, 2025 17:07
@Darksonn
Copy link
Contributor

It doesn't matter for me, but should we make this change gradually by supporting both for some time?

@1c3t3a
Copy link
Member Author

1c3t3a commented Jun 18, 2025

It doesn't matter for me, but should we make this change gradually by supporting both for some time?

That's possible for sure, I put stuff into different commits for that reason and I can just drop the middle commit to have both exist in parallel.

What's the reason for having both at the same time? An easier migration period?

@traviscross
Copy link
Contributor

traviscross commented Jun 19, 2025

We generally do not make such efforts to migrate usage of nightly features without a specific known good reason to do so. (Let us know of course if there is one, e.g. if this affects RfL in some way.)

@bors
Copy link
Collaborator

bors commented Jun 20, 2025

☔ The latest upstream changes (presumably #142770) made this pull request unmergeable. Please resolve the merge conflicts.

@ojeda
Copy link
Contributor

ojeda commented Jul 1, 2025

e.g. if this affects RfL in some way.

We don't use #[no_sanitize] in mainline (nor in -next) at the moment.

@1c3t3a
Copy link
Member Author

1c3t3a commented Jul 14, 2025

I rebased the change to make review easier.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that if this is a built-in attribute, then IIUC #[sanitize] (even if unstably gated) could introduce new name resolution ambiguities with user macros or derive macro attributes and thus technically break existing user code. See for instance #143834. cf. #134963.

// tests/ui/whatever_subdir/test.rs

//@ proc-macro: my_derive.rs
//@ edition: 2024

use my_derive::MyDerive;

#[derive(MyDerive)]
#[sanitize]
//~^ ERROR `sanitize` is ambiguous
pub struct Foo;

fn main() {}
// tests/ui/whatever_subdir/auxiliary/my_derive.rs

//@ edition: 2024

extern crate proc_macro;
use proc_macro::{Span, TokenStream};

#[proc_macro_derive(
    MyDerive,
    attributes(
        sanitize
    )
)]
pub fn derive_custom(_item: TokenStream) -> TokenStream {
    TokenStream::new()
}

You can still observe this for #[no_sanitize] too, if you switch sanitize for no_sanitize in the above test case.

error: malformed `no_sanitize` attribute input
  --> /home/joe/repos/rust/tests/ui/bitbuffer_derive/test.rs:7:1
   |
LL | #[no_sanitize]
   | ^^^^^^^^^^^^^^ help: must be of the form: `#[no_sanitize(address, kcfi, memory, thread)]`

error[E0659]: `no_sanitize` is ambiguous
  --> /home/joe/repos/rust/tests/ui/bitbuffer_derive/test.rs:7:3
   |
LL | #[no_sanitize]
   |   ^^^^^^^^^^^ ambiguous name
   |
   = note: ambiguous because of a name conflict with a builtin attribute
   = note: `no_sanitize` could refer to a built-in attribute

error[E0658]: the `#[no_sanitize]` attribute is an experimental feature
  --> /home/joe/repos/rust/tests/ui/bitbuffer_derive/test.rs:7:1
   |
LL | #[no_sanitize]
   | ^^^^^^^^^^^^^^
   |
   = note: see issue #39699 <https://github.com/rust-lang/rust/issues/39699> for more information
   = help: add `#![feature(no_sanitize)]` to the crate attributes to enable
   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0658, E0659.
For more information about an error, try `rustc --explain E0658`.

Or just

macro_rules! sanitize {
    () => {
        /* .. */
    };
}

pub(crate) use sanitize; // `use` here becomes ambiguous

fn main() {}

I imagine no_sanitize is much less likely to collide with something a user would write (not an argument against this change, just an observation re. technically a breaking change).

I don't know how much of a concern for breakage this is in practice, a crater run may or may not be a good idea. If the breakage is deemed acceptable, then this should get relnotes.

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

☔ The latest upstream changes (presumably #143919) made this pull request unmergeable. Please resolve the merge conflicts.

@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from bb98641 to a466546 Compare July 14, 2025 16:55
@rust-log-analyzer

This comment has been minimized.

@traviscross
Copy link
Contributor

@rustbot labels +T-lang +needs-fcp
cc @rust-lang/lang

Given that this is breaking, we should review and FCP. Let's do that crater run, since we'll need it for our review.

@bors2 try

@craterbot craterbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 19, 2025
@traviscross
Copy link
Contributor

The regressions all look spurious to me.

@1c3t3a
Copy link
Member Author

1c3t3a commented Jul 22, 2025

I agree, also running rg -U --multiline-dotall -e 'no_sanitize is ambiguous' --files-with-matches | cut -d/ -f1 | counts doesn't return any results, so it seems like nobody redefined this attribute so far.

1c3t3a added 2 commits July 23, 2025 13:39
This change implements the #[sanitize(..)] attribute, which opts to
replace the currently unstable #[no_sanitize]. Essentially the new
attribute works similar as #[no_sanitize], just with more flexible
options regarding where it is applied. E.g. it is possible to turn
a certain sanitizer either on or off:
`#[sanitize(address = "on|off")]`

This attribute now also applies to more places, e.g. it is possible
to turn off a sanitizer for an entire module or impl block:
```rust
\#[sanitize(address = "off")]
mod foo {
    fn unsanitized(..) {}

    #[sanitize(address = "on")]
    fn sanitized(..) {}
}

\#[sanitize(thread = "off")]
impl MyTrait for () {
    ...
}
```

This attribute is enabled behind the unstable `sanitize` feature.
This removes the #[no_sanitize] attribute, which was behind an unstable
feature named no_sanitize. Instead, we introduce the sanitize attribute
which is more powerful and allows to be extended in the future (instead
of just focusing on turning sanitizers off).
@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from 9ee7903 to 758fd0b Compare July 23, 2025 14:49
@rustbot
Copy link
Collaborator

rustbot commented Jul 23, 2025

Some changes occurred in tests/codegen-llvm/sanitizer

cc @rcvalle

@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from 758fd0b to 63214a8 Compare July 23, 2025 14:57
@rust-log-analyzer

This comment has been minimized.

@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from 63214a8 to d956109 Compare July 23, 2025 15:57
@rust-log-analyzer

This comment has been minimized.

…ress

To do it the same as how clang disables address sanitizer, we now
disable ASAN on sanitize(kernel_address = "off") and KASAN on
sanitize(address = "off").

The same was added to clang in https://reviews.llvm.org/D44981.
@1c3t3a 1c3t3a force-pushed the sanitize-off-on branch from d956109 to dc8bff1 Compare July 24, 2025 08:14
@1c3t3a
Copy link
Member Author

1c3t3a commented Jul 24, 2025

Is there anything else needed for this change before landing? To summarize:

  • This switches directly from no_sanitize to sanitize. Rust for Linux is currently not using the attribute and the crater run revealed two users that depend on it: https://github.com/keepitupkitty/ouma and https://github.com/lz520520/rust-1.75-ollvm, both of which seem unmaintained.
  • There is also no collision between the new sanitize attribute and other, custom macro implementations, according to the data from the crater run.

@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2025
@traviscross
Copy link
Contributor

Given the clean crater run and the likelihood that we will want to use this attribute for this purpose, I propose that we accept this breakage and the use of this attribute for this experimental work.

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented Jul 24, 2025

Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jul 24, 2025
@RalfJung
Copy link
Member

FWIW, #[sanitize(address = "off")] seems a bit inconsistent with #[target_feature(enable = "avx")], in terms of how we represent "turn this thing on/off". But I am not sure if it's worth trying to be consistent here -- having an ad-hoc list in the string value of an attribute key (i.e., what target_feature does) seems worse than using the fact that attributes already support lists of key-value pairs (i.e., what sanitize does).

I assume the syntax to disable multiple sanitizers is #[sanitize(address = "off", thread = "off")]?

@1c3t3a
Copy link
Member Author

1c3t3a commented Jul 25, 2025

This attribute is more akin to the coverage attribute #[coverage(off)]. If we were to go with the same approach as target_feature we'd have something like the following syntax to disable multiple sanitizers #[sanitize(off = "address,thread", on = "cfi,memory")] or #[sanitize(off = "address", off = "thread", on = "cfi", on = "memory")].

I assume the syntax to disable multiple sanitizers is #[sanitize(address = "off", thread = "off")]?

Yes indeed!

@RalfJung
Copy link
Member

If we were to go with the same approach as target_feature we'd have something like the following syntax to disable multiple sanitizers #[sanitize(off = "address,thread", on = "cfi,memory")] or #[sanitize(off = "address", off = "thread", on = "cfi", on = "memory")].

Yeah... that looks terrible.^^ But it seemed worth mentioning in terms of evaluating consistency across the language.

@tmandry
Copy link
Member

tmandry commented Jul 26, 2025

I wish we had some philosophy for how to spell these attributes. Why should it be #[sanitize(address = "off")] and not #[sanitize(address(off))] or #[sanitize(address = off)]?

Since the lang team hasn't developed our own answers to these questions though, I'm inclined not to block. There's not a practical reason to spell it one way or the other, and we can make these things more consistent in a future edition.

@rfcbot reviewed

@traviscross
Copy link
Contributor

traviscross commented Jul 26, 2025

Since the lang team hasn't developed our own answers to these questions though, I'm inclined not to block. There's not a practical reason to spell it one way or the other, and we can make these things more consistent in a future edition.

Note in particular that we're not stabilizing this or even agreeing necessarily to the details of the syntax. We're just accepting the breaking change required to use the sanitize attribute for this experimental work in nightly (on the presumption that we are likely to use this attribute when later stabilizing).

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jul 26, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented Jul 26, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

atomic operations to avoid reporting false positives and provide meaning full
stack traces.

This attribute was previously named `no_sanitized`.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This attribute was previously named `no_sanitized`.
This attribute was previously named `no_sanitize`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-meta Area: Issues & PRs about the rust-lang/rust repository itself A-rustc-dev-guide Area: rustc-dev-guide disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. I-lang-nominated Nominated for discussion during a lang team meeting. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-lang Relevant to the language team
Projects
None yet
Development

Successfully merging this pull request may close these issues.